package com.yourcompany.api.entity;

import com.baomidou.mybatisplus.annotation.TableName;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.Builder;
import lombok.NoArgsConstructor;
import lombok.AllArgsConstructor;
import com.yourcompany.common.utils.WebUtils;

import java.time.LocalDateTime;

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode(callSuper = true)
@TableName("login_log")
@Schema(description = "登录日志")
public class LoginLog extends BaseEntity {

    @Schema(description = "用户ID")
    private Long userId;

    @Schema(description = "用户名")
    private String username;

    @Schema(description = "登录方式：account-账号 mobile-手机号 weixin-微信")
    private String loginType;

    @Schema(description = "登录IP")
    private String loginIp;

    @Schema(description = "登录地点")
    private String loginLocation;

    @Schema(description = "登录时间")
    private LocalDateTime loginTime;

    @Schema(description = "用户代理")
    private String userAgent;

    @Schema(description = "设备ID")
    private String deviceId;

    @Schema(description = "设备类型")
    private String deviceType;

    @Schema(description = "操作系统")
    private String os;

    @Schema(description = "浏览器")
    private String browser;

    @Schema(description = "APP版本号")
    private String appVersion;

    @Schema(description = "登录状态：0-失败 1-成功")
    private Integer status;

    @Schema(description = "提示消息")
    private String msg;

    // 静态工厂方法，替代默认的builder()
    public static LoginLogBuilder loginLogBuilder() {
        return builder()
                .loginIp(WebUtils.getIpAddr())
                .loginLocation(WebUtils.getLocationByIp(WebUtils.getIpAddr()))
                .loginTime(LocalDateTime.now())
                .userAgent(WebUtils.getUserAgent());
    }
}